





function fixaddress(item){
    let address = $(item).find('.saved-address').html();
    $('#receive-address').val(address);
}







$(function () {

    let itemId = _common_util.getURLParam('itemId');
    let itemcount = _common_util.getURLParam('itemcount');

    console.log(itemId+" or "+itemcount);
    $.ajax({
        type: 'GET',
        url : _common_util.getServerURL('commodity/item_detail?itemId=')+itemId,
        success:function (res) {
            console.log(res);
            if (res.status === 0){
                let name = res.data.itemName;
                let nameAndCount = name +"✖️"+ itemcount;
                $('#product-name-count').html(nameAndCount);
                $('#product-price').html(res.data.price);
                let total = res.data.price * itemcount ;
                $('#totalprice').html(total);
            }

        }
    });

    $.ajax({
        type: 'GET',
        url: _common_util.getServerURL('admin/get_account'),
        success:function (res) {
            if (res.status === 0){
                let name = res.data.username;
                $('#receive-name').val(res.data.username);
                $('#receive-phone').val(res.data.phoneNumber);
                $.each(res.data.addressList,function (i,item) {
                    console.log(item);
                    str= '                          <div class="address-item"  onclick="fixaddress(this)">\n' +
                        '                                        <div>\n' +
                        '                                            <span>'+name+'</span>\n' +
                        '                                            <span>'+res.data.phoneNumber+'</span>\n' +
                        '                                        </div>\n' +
                        '                                        <div>\n' +
                        '                                            <span class="saved-address">'+item+'</span>\n' +
                        '                                        </div>\n' +
                        '                                        <div>\n' +
                        '                                            <a class="btn-info" style="display: inline-block" >编辑</a>\n' +
                        '                                            <a class="btn-info" style="display: inline-block">删除</a>\n' +
                        '                                        </div>\n' +
                        '\n' +
                        '                                    </div>';

                    $('.address-list').append(str);
                })
            }
        }
    })
    $('#createorder').on('click',function () {
        $.ajax({
            type:'POST',
            url : _common_util.getServerURL('order/purchase_order'),
            dataType:'json',
            data:{
                'orderName':'订单名',
                'itemId' : itemId,
                'itemNumber' : itemcount,
                'unitPrice': $('#product-price').html(),
                'totalPrice': $('#totalprice').html(),
                'payment' : '1',
                'cAddress' : $('#receive-address').val(),
                'cPhone' : $('#receive-phone').val()

            },
            success:function (res) {
                console.log(res);
                if (res.status === 0 ){
                    window.location.href = 'order.html?orderId='+res.data.orderId;
                }else if (res.status === 3){
                    window.wxc.xcConfirm('尚未登录，请先登录','info',function () {
                        window.location.href='my-account.html';
                    })
                }else if (res.status ===20 ){
                    window.wxc.xcConfirm('库存不足','info');
                }
            }
        })
    })


});


